﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：InvalidPurchaseGoodsList.cs
    // 文件功能描述：采购目录维护,无效采购目录，可以恢复采购目录
    // 创建标识：吕海乐2009-05-14
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using System.Data;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.Procurement
{
    public partial class InvalidPurchaseGoodsList : BasePage
    {
        public InvalidPurchaseGoodsList()
        {
            this.ModuleNo = Module.产品库管理.ToString();
        }

        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        IBLL_HospitalProcureCatalog ihp = Factory.BLL_HospitalProcureCatalog();
        IBLL_Distribution id = Factory.BLL_Distribution();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HZ.DrugTrade.Web.Function.IsThreeLevel())//项目查询条件仅三级医院可见
            {
                this.ddlProjectid.Visible = false;
                this.lblProjectid.Visible = false;
            }
            if (!IsPostBack)
            {
                //绑定所有项目
                //ProcurementProject procurementProject = new ProcurementProject();
                //procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                //procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                //procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                //procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                //int temp;
                //IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                //ddlProject.DataSource = projectList;
                //ddlProject.DataTextField = "ProjectName";
                //ddlProject.DataValueField = "ProjectID";
                //ddlProject.DataBind();
                //ListItem item = new ListItem();
                //item.Text = Constants.ALL_ITEM_TEXT;
                //item.Value = Constants.ALL_ITEM_VALUE;
                //ddlProject.Items.Insert(0, item);
                //绑定采购目录
                setBind();
            }
        }

        /// <summary>
        /// 绑定医疗机构自身的采购目录
        /// </summary>
        private void setBind()
        {
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            Model.Query.HospitalProcureCatalog qHospitalCatalog = GetCondition();
            IList<Model.View.HospitalProcureCatalog> hosiptalCatalogList = ihp.GetHospitalProcureCatalog(qHospitalCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            DataTable hospitalCatalog = HZ.Data.ModelConvert.GetDataTableFromModel(hosiptalCatalogList);
            //hospitalCatalog.Columns.Add("Company_PS");
            //hospitalCatalog.Columns.Add("CompanyID_PS");

            ////从配送关系中取出配送企业
            //Model.Query.DistributionRelation qDistributionRelation = new HZ.DrugTrade.Model.Query.DistributionRelation();
            //qDistributionRelation.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            //qDistributionRelation.ProcureCatalog_IsUsing = true;
            //qDistributionRelation.DistributionIsUsing = true;
            ////qDistributionRelation.Hospital_IsUsing = true;
            //qDistributionRelation.CompanyName_PS = txtCompany_PS.Text.Trim();
            //qDistributionRelation.SortFields = "ProductName asc";
            //IList<Model.View.DistributionRelation> distributionList = id.GetDistributionByWhere(qDistributionRelation, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            //DataTable distributionCatalog = HZ.Data.ModelConvert.GetDataTableFromModel(distributionList);
            //for (int i = 0; i < hospitalCatalog.Rows.Count; i++)
            //{
            //    for (int j = distributionCatalog.Rows.Count - 1; j >= 0; j--)
            //    {
            //        if (hospitalCatalog.Rows[i]["ProcureCatalogID"].Equals(distributionCatalog.Rows[j]["ProcureCatalogID"]))
            //        {
            //            hospitalCatalog.Rows[i]["Company_PS"] = distributionCatalog.Rows[j]["CompanyName_PS"];
            //            hospitalCatalog.Rows[i]["CompanyID_PS"] = distributionCatalog.Rows[j]["CompanyID_PS"];
            //        }
            //    }
            //}
            gvwProduceCatalog.PageSize = AspNetPager1.PageSize;
            gvwProduceCatalog.DataSource = hospitalCatalog;
            gvwProduceCatalog.DataBind();
        }

        /// <summary>
        /// 获取查询条件(已撤废的采购目录)
        /// </summary>
        /// <returns></returns>
        private Model.Query.HospitalProcureCatalog GetCondition()
        {
            Model.Query.HospitalProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.HospitalProcureCatalog();
            procureCatalog.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            procureCatalog.Companyname_Sc = txtProduceCompany_SC.Text.Trim();
            procureCatalog.Companyname_Tb = txtCompany_TB.Text.Trim();
            procureCatalog.GoodsID = txtGoodsid.Text.Trim();
            procureCatalog.Companyid_Ps = txtCompany_PS.Text.Trim();
            //procureCatalog.PinyinCode = this.txtPinyinCode.Text.Trim().ToUpper();
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.PinyinCode = this.txtpinyincode.Text.Trim();
            //procureCatalog.ProjectID = ddlProject.SelectedItem.Value;
            procureCatalog.Unit = txtUnit.Text.Trim();
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();
            //procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            if (this.ddlProjectid.Visible != false)
            {
                procureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            }
            else
            {
                procureCatalog.ProjectID = "0001";
            }
            procureCatalog.Material = txtMaterial.Text.Trim();
            //procureCatalog.SortName = txtProductType.Text;
            //string orgID = CurrentAccount.GetUser().Org.OrgID;
            //bool flag = ipb.GetIsJiCeng(orgID);//判断是否基层医疗机构 add by zdc
            //if (flag)//盟市添加的只能采购基本药物和内蒙基本药物 zdc
            //{
            //    procureCatalog.IsJiCeng = 0;
            //}
            procureCatalog.SortFields = "GoodsName asc,goodsid";
            procureCatalog.Hospital_IsUsing = false;
            return procureCatalog;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            //setBind();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 简单查询与高级查询间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtMaterial.Text = "";
                //txtPinyinCode.Text = "";
                txtProduceCompany_SC.Text = "";
                //txtProductType.Text = "";
                txtUnit.Text = "";
                txtGoodsName.Text = "";
                txtCompany_PS.Text = "";
                //ddlProject.SelectedIndex = -1;
            }
        }

        protected void btnRecover_Click(object sender, EventArgs e)
        {
            List<HospitalProcureCatalog> hospitalCatalogList = new List<HospitalProcureCatalog>();
            //获取所有选择的目录
            string ids = Request["checkbox1"];
            if (!string.IsNullOrEmpty(ids))
            {
                string[] arrayID = ids.Split(',');
                foreach (string item in arrayID)
                {
                    HospitalProcureCatalog catalog = new HospitalProcureCatalog();
                    catalog.HospitalID = CurrentAccount.GetUser().Org.OrgID;
                    catalog.ProcureCatalogID = item;
                    catalog.IsUsing = true;
                    hospitalCatalogList.Add(catalog);
                }

                //调取方法，恢复所选的采购目录 
                if (ihp.ValidateHospitalProcureCatalog_Batch(hospitalCatalogList) == 1)
                {
                    setBind();
                }
                else
                {
                    PageError("恢复失败！", this.ReturnUrl);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选择一个目录！", this);
            }
        }
        /// <summary>
        /// 无效品种导出 2009-11-14
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {

            Model.Query.HospitalProcureCatalog qHospitalCatalog = GetCondition();
            //int totalCount = 0;

            //IList<Model.View.HospitalProcureCatalog> hosiptalCatalogList = ihp.GetHospitalProcureCatalog(qHospitalCatalog, 1, 0, out totalCount);
            //DataTable dt = HZ.Data.ModelConvert.GetDataTableFromModel(hosiptalCatalogList);
            //HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwProduceCatalog);
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.HospitalProcureCatalog>(qHospitalCatalog, true);
            string sql = "";
            if (this.ddlProjectid.Visible != false)
            {
                sql = string.Format(@"SELECT '‘'||Goodsid 产品编号,sortname 目录名称,GoodsName 产品名称,Outlookc 规格型号,Material 材质,Count 最小包装数量,Unit 最小计量单位,CompanyName_SC_TB 生产企业代理,QualityLevel 产品类别,ProcurePrice 自主报价,Price 采购价,ProjectName 项目,CompanyName_PS 配送企业 FROM View_HospitalProcureCatalog  WHERE 1=1 {0}", where);
            }
            else
            {
                sql = string.Format(@"SELECT Goodsid 产品编号,sortname 目录名称,GoodsName 产品名称,Outlookc 规格型号,Material 材质,Count 最小包装数量,Unit 最小计量单位,CompanyName_SC_TB 生产企业代理,QualityLevel 产品类别,ProcurePrice 采购限价,Price 采购价,ProjectName 项目,CompanyName_PS 配送企业 FROM View_HospitalProcureCatalog  WHERE 1=1 {0}", where);
            }
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "无效采购目录.xls");
        }

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    if (ddlProjectid.SelectedItem.Value == "0002")
                    {
                        e.Row.Cells[11].Text = "自主报价";
                    }
                    else
                    {
                        e.Row.Cells[11].Text = "采购限价";
                    }
                }
            }
        }

    }
}
